const fs = require('fs')
const path = require('path')

const accessLogsPath = path.join(__dirname, 'logs/access.log')
const warnLogsPath = path.join(__dirname, 'logs/warn.log')
const errorLogsPath = path.join(__dirname, 'logs/error.log')
const formatter = new Intl.DateTimeFormat('zh-CN', {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: false // 使用 24 小时制
})
const formattedDate = (date) => formatter.format(date)
  .replace(/\//g, '-') // 将斜杠替换为短横
  .replace(/(\d{2}):(\d{2}):(\d{2})/, '$1:$2:$3') // 可选，确保格式一致

exports.warnLog = (message) => {
  fs.appendFileSync(warnLogsPath, `warn ${formattedDate(Date.now())} - ${message}\n`)
}

exports.errorLog = (message) => {
  fs.appendFileSync(errorLogsPath, `error ${formattedDate(Date.now())} - ${message}\n`)
}

exports.accessLog = (message) => {
  fs.appendFileSync(accessLogsPath, `info ${formattedDate(Date.now())} - ${message}\n`)
}
